package com.aidex.admin.controller.system;

import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.domain.model.LoginUser;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.query.QueryGenerator;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.system.domain.SysThemeConfig;
import com.aidex.system.service.SysThemeConfigService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * 用户主题
 */
@RestController
@RequestMapping("/sysThemeConfig/sysThemeConfig")
public class SysThemeConfigController extends BaseController<SysThemeConfigService> {
    /**
     * 查询用户主题列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysThemeConfig:list')")
    @GetMapping("/list")
    public R page(SysThemeConfig config,
                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                  HttpServletRequest req) {
        QueryWrapper<SysThemeConfig> queryWrapper = QueryGenerator.initQueryWrapper(config, req.getParameterMap());
        Page<SysThemeConfig> page = new Page<>(pageNo, pageSize);
        IPage<SysThemeConfig> pageList = service.selectPage(page, queryWrapper);
        return R.data(pageList);
    }

    /**
     * 获取用户主题详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sysThemeConfig:query')")
    @GetMapping(value = "/{id}")
    public R detail(@PathVariable("id") String id) {
        return R.data(service.selectById(id));
    }

    @GetMapping(value = "/getInfoByTableKey/{tableKey}")
    public R getInfoByTableKey(@PathVariable("tableKey") String tableKey) {
        return R.success();
    }

    /**
     * 新增用户主题
     */
    @Log(title = "用户主题", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated SysThemeConfig tableConfig) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tableConfig.setUserId(loginUser.getUser().getId());
        service.insert(tableConfig);
        return R.data(tableConfig);
    }

    /**
     * 修改用户主题
     */
    @Log(title = "用户主题", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated SysThemeConfig tableConfig) {
        service.updateById(tableConfig);
        return R.data(tableConfig);
    }

    /**
     * 删除用户主题
     */
    @PreAuthorize("@ss.hasPermi('system:sysThemeConfig:remove')")
    @Log(title = "用户主题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tableKeys}")
    public R remove(@PathVariable String[] ids) {
        return R.status(service.deleteBatchIds(Arrays.asList(ids)));
    }

    /**
     * 导出用户主题列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysThemeConfig:export')")
    @Log(title = "用户主题", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(SysThemeConfig config, HttpServletRequest req) {
        QueryWrapper<SysThemeConfig> queryWrapper = QueryGenerator.initQueryWrapper(config, req.getParameterMap());
        List<SysThemeConfig> list = service.selectList(queryWrapper);
        ExcelUtil<SysThemeConfig> util = new ExcelUtil<SysThemeConfig>(SysThemeConfig.class);
        return util.exportExcel(list, "sysThemeConfig");
    }

}
